package com.zhangjm.controller;

import com.zhangjm.api.ResultApi;
import com.zhangjm.dao.HouseDao;
import com.zhangjm.pojo.House;
import com.zhangjm.pojo.enums.DecorateEnum;
import com.zhangjm.pojo.vo.SearchVo;
import com.zhangjm.service.CitysService;
import com.zhangjm.uploadspringbootstarter.autoconfig.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Objects;
import java.util.UUID;

@RestController
@RequestMapping("/house")
public class HouseController {

    @Autowired
    private HouseDao houseDao;

    @Autowired
    private CitysService citysService;

    @Autowired
    private UploadService uploadService;

    /**
     * 条件查询
     * @param current 当前页
     * @param size 当前页展示的条数
     * @param vo   条件查询需要的字段
     * @return
     */
    @RequestMapping("list")
    public ResultApi list(@RequestParam(defaultValue = "1") Integer current,
                          @RequestParam(defaultValue = "3") Integer size,
                          SearchVo vo) {
        PageRequest pageRequest = PageRequest.of(current - 1, size);
        //根据省份
        if (vo.getPid() != 0) {
            Page<House> byProvince = houseDao.findByProvince(vo.getPid(), pageRequest);
            return ResultApi.success(byProvince);
        }
        //根据几室
        if (vo.getRoom() != 0 && vo.getRoom() <= 4) {
            Page<House> byRoom = houseDao.findByRoom(vo.getRoom(), pageRequest);
            return ResultApi.success(byRoom);
        } else if (vo.getRoom() > 4) {
            Page<House> byRoomGreaterThan = houseDao.findByRoomGreaterThanEqual(vo.getRoom(), pageRequest);
            return ResultApi.success(byRoomGreaterThan);
        }
        //根据租金
        if (vo.getSrent().intValue() != 0 && vo.getErent().intValue() != 0) {
            Page<House> byRentGreaterThanEqualAndRentLessThanEqual = houseDao.findByRentGreaterThanEqualAndRentLessThanEqual(vo.getSrent(), vo.getErent(), pageRequest);
            return ResultApi.success(byRentGreaterThanEqualAndRentLessThanEqual);
        }
//        System.out.println("room!!!!!!!!!!!!!!!!!!!"+srent);
//        System.out.println("room!!!!!!!!!!!!!!!!!!!"+erent);
//        System.out.println("room!!!!!!!!!!!!!!!!!!!"+room);
//        System.out.println("room!!!!!!!!!!!!!!!!!!!"+pid);
//        if(room != 0){
//            if( pid != 0 ){
//                if(srent.intValue() != 0 && erent.intValue() != 0){
//                    Page<House> byRentGreaterThanEqualAndRentLessThanEqualOrRoomOrProvince = houseDao.findByRentGreaterThanEqualAndRentLessThanEqualAndRoomAndProvince(srent,erent,room,pid, pageRequest);
//                     Example<Object> of = Example.of(byRentGreaterThanEqualAndRentLessThanEqualOrRoomOrProvince);
//                    return ResultApi.success(of);
//                }
//            }
//        }
        return ResultApi.success(houseDao.findAll(pageRequest));
    }


    /** 图片上传工具类 **/
    @RequestMapping("/upload")
    private ResultApi upload(MultipartFile file) {
        if (!file.isEmpty()) {
            //原始文件名称
            String originalFilename = file.getOriginalFilename();
            //文件名的唯一性
            String fileName = UUID.randomUUID() + "_" + originalFilename;
            //设置上传的路劲
            File destFile = new File("D:/pic/", fileName);
            //判断目录是否存在
            if (!destFile.getParentFile().exists()) {
                //则创建目录
                destFile.getParentFile().mkdir();
            }
            //保存文件
            try {
                file.transferTo(destFile);
                return ResultApi.success("http://localhost:20001/img/" + fileName);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return ResultApi.error(null);
    }

    /** 获取所有城市 **/
    @GetMapping("getAllCity")
    public ResultApi getAllCity() {
        return ResultApi.success(citysService.getAllMenu());
    }

    /** 添加房屋 **/
    @PostMapping("insert")
    public ResultApi insert(@RequestBody House house) {
        return ResultApi.success(houseDao.save(house));
    }

    /**
     * 获取装修类型枚举
     */
    @GetMapping("/getDecorateEnum")
    public ResultApi getDecorateEnum() {
        return ResultApi.success(DecorateEnum.values());
    }

    /** 修改房屋 **/
    @PostMapping("/update")
    public ResultApi update(@RequestBody House house) {
        return ResultApi.success(houseDao.save(house));
    }

    /** 房屋批删 **/
    @GetMapping("deleteBatch")
    public ResultApi delete(Integer[] ids) {
        Objects.requireNonNull(ids);
        for (Integer id : ids) {
            House house = new House();
            house.setId(id);
            houseDao.delete(house);
        }
        return ResultApi.success(null);
    }
}
